package com.example.patients;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.patients.adapter.Adapter_del;
import com.example.patients.database.DbPatients;
import com.example.patients.entity.PatientsBean;

import java.util.ArrayList;
import java.util.List;

public class DeletePatientsActivity extends AppCompatActivity {
    ListView listView;//列表
    List<PatientsBean> mData;//数据源
    Adapter_del adapter;//适配器
    DbPatients dbPatients;//数据库


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_patients);//控件绑定
        listView = findViewById(R.id.del_lv);
        dbPatients = new DbPatients(this);
        mData = new ArrayList<>();
        //System.out.println("长度为:" + mData.size());
        //设置适配器：加载每一行数据到列表当中
        adapter = new Adapter_del(this, mData);
        listView.setAdapter(adapter);
        loadData();
        setLVClickListener();
    }

    /**
     * listView长按事件
     */
    private void setLVClickListener() {
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                PatientsBean patientsBean = mData.get(position);
                deleteItem(patientsBean);
                return false;
            }
        });
    }
    /**
     * 长按删除
     * */
    private void deleteItem(final PatientsBean patientsBean) {
        final String delNumber = patientsBean.getNumber();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示信息").setMessage("您确定为病人办理出院吗？")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DbPatients.deleteItemFromPatientByNumber(delNumber);
                        mData.remove(patientsBean);    //实时刷新，从数据源删除
                        adapter.notifyDataSetChanged();
                        dbPatients.updateTotalPatientCount(dbPatients.getTotalPatientCount() - 1);//更新总人数
                    }
                });
        builder.create().show();
    }
    /**
     * 显示病人信息列表
     * */
    private void loadData() {
        List<PatientsBean> list = DbPatients.search();
        mData.clear();
        mData.addAll(list);
        adapter.notifyDataSetChanged();
    }
    /**
     * 按钮点击事件
     * */
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.iv_back){
            Intent intent = new Intent(DeletePatientsActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
